package mm.chenchen.modules.host.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import mm.chenchen.modules.host.entity.model.SshCommandModel;
import mm.chenchen.modules.script.dao.model.request.ScriptMirroringQuery;
import mm.chenchen.modules.sys.controller.AbstractController;
import mm.chenchen.modules.sys.entity.SysUserEntity;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import mm.chenchen.modules.host.entity.HostInfo;
import mm.chenchen.modules.host.service.HostInfoService;
import mm.chenchen.modules.host.dao.model.request.HostInfoQuery;
import mm.chenchen.utils.PageUtils;
import mm.chenchen.utils.R;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

/**
 * @author
 * @email
 * @date 2021-08-12 11:16:56
 */

@RestController
@Api(tags = "主机管理")
@RequestMapping("host/hostinfo")
@CrossOrigin
public class HostInfoController extends AbstractController {
    @Autowired
    private HostInfoService service;

    /**
     * 列表
     */
    @ApiOperation(value = "分页检索", notes = "")
    @PostMapping("/list")
    public R findPage(@RequestBody HostInfoQuery param) {
        return service.findPage(PageUtils.getPage(param), param);
    }
    @ApiOperation(value = "下拉选择", notes = "")
    @PostMapping("drop/down/list")
    //@RequiresPermissions("reptile:scriptpythonmirroring:list")
    public R dropDownList(@RequestBody ScriptMirroringQuery param) {
        QueryWrapper<HostInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().like(HostInfo::getName,param.getName());
        return R.ok().setData(service.list(queryWrapper));
    }

    /**
     * 信息
     */
    @ApiOperation(value = "单个检索 ", notes = "")
    @GetMapping(value = "/{objId}", produces = "application/json")
    //@RequiresPermissions("host:hostinfo:info")
    public R getById(@ApiParam(name = "objId", value = "") @PathVariable Long objId) {
        return service.getById(objId);
    }

    /**
     * 保存
     */
    @ApiOperation(value = "新增", notes = "")
    @PostMapping(value = "", produces = "application/json")
    //@RequiresPermissions("host:hostinfo:save")
    public R save(@RequestBody HostInfo param) {
        param.setOperId(((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUserId());
        return service.saveInfo(param);
    }

    /**
     * 修改
     */
    @ApiOperation(value = "更新", notes = "")
    @PutMapping(value = "/{objId}", produces = "application/json")
    //@RequiresPermissions("host:hostinfo:update")
    public R update(@ApiParam(name = "objId", value = "") @PathVariable Long objId, @RequestBody HostInfo param) {
        param.setId(objId);
        param.setOperId(((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUserId());
        return service.update(param);
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除", notes = "")
    @DeleteMapping(value = "/{objId}", produces = "application/json")
    //@RequiresPermissions("host:hostinfo:delete")
    public R deleteById(@ApiParam(name = "objId", value = "") @PathVariable Long objId) {
        return service.deleteById(objId);
    }

    /**
     * 删除
     */
    @ApiOperation(value = "下拉列表可筛选", notes = "")
    @PostMapping(value = "/drop/list", produces = "application/json")
    //@RequiresPermissions("host:hostinfo:delete")
    public R deleteById(@RequestBody HostInfo param) {
        QueryWrapper<HostInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().like(HostInfo::getName, param.getName());
        return R.ok().setData(service.list(queryWrapper));
    }

    /**
     * 查看环境的软件详情
     * @return
     */
    @ApiOperation(value = "查看主机环境信息 ", notes = "")
    @GetMapping(value = "/environment/{objId}", produces = "application/json")
    public R environmentDetail(@ApiParam(name = "objId", value = "") @PathVariable Long objId) throws Exception{
        return service.environmentDetail(objId);
    }

    /**
     * 查看环境的软件详情
     * @return
     */
    @ApiOperation(value = "在主机上执行ssh命令 ", notes = "")
    @PostMapping(value = "/command", produces = "application/json")
    public R sshCommand(@RequestBody SshCommandModel param) throws Exception{
        return service.sshCommand(param);
    }


}
